package api

import (
	"buding-ai/internal/service"
	"buding-ai/internal/web/dto"
	"buding-ai/internal/web/response"
	"buding-ai/pkg/utils/paginate"
	"errors"
	"github.com/gin-gonic/gin"
)

type AgentModelProvidersApi struct {
	router *gin.RouterGroup
	srv    *service.AgentModelProviderService
	BaseApi
}

func NewAgentModelProvidersApi(router *gin.RouterGroup) *AgentModelProvidersApi {
	api := &AgentModelProvidersApi{
		router: router,
		srv:    service.NewAgentModelProviderService(),
	}
	api.router.POST("", api.Add)
	api.router.GET("/:id", api.Info)
	api.router.DELETE("/:id", api.Delete)
	api.router.PUT("", api.Update)
	api.router.GET("/page", api.Page)
	api.router.PUT("/default/:id", api.SetDefault)
	api.router.GET("/default", api.GetDefault)
	api.router.GET("/active", api.GetActive)
	api.router.GET("/modelList", api.ModelList)
	return api
}

func (api *AgentModelProvidersApi) Add(ctx *gin.Context) {
	var providerDTO dto.AgentModelProviderDTO
	if err := ctx.ShouldBindJSON(&providerDTO); err != nil {
		response.ParamBindError(ctx, err)
		return
	}
	if err := api.srv.Create(providerDTO); err != nil {
		response.InternalError(ctx, "创建提供商失败", err)
		return
	}
	response.SuccessMsg(ctx, "添加提供商成功")
}

func (api *AgentModelProvidersApi) ModelList(ctx *gin.Context) {
	var baseUrl string
	var token string
	var flag bool
	if baseUrl, flag = api.GetQuery(ctx, "baseUrl"); !flag {
		response.ParamBindError(ctx, errors.New("供应商url获取失败"))
		return
	}
	if token, flag = api.GetQuery(ctx, "authToken"); !flag {
		response.ParamBindError(ctx, errors.New("供应商token获取失败"))
		return
	}
	var result []string
	var err error
	if result, err = api.srv.GetModelList(baseUrl, token); err != nil {
		response.InternalError(ctx, "获取数据失败", err)
		return
	}
	response.SuccessWithData(ctx, result)

}

func (api *AgentModelProvidersApi) Info(ctx *gin.Context) {
	id, err := api.GetParamUint(ctx, "id")
	if err != nil {
		response.ParamBindError(ctx, err)
		return
	}
	resp, err := api.srv.GetById(id)
	if err != nil {
		response.InternalError(ctx, "查询失败", err)
		return
	}
	response.SuccessWithData(ctx, resp)
}

func (api *AgentModelProvidersApi) Delete(ctx *gin.Context) {
	id, err := api.GetParamUint(ctx, "id")
	if err != nil {
		response.ParamBindError(ctx, err)
		return
	}
	if err := api.srv.DeleteById(id); err != nil {
		response.InternalError(ctx, "删除失败：", err)
		return
	}
	response.SuccessMsg(ctx, "删除成功")
}

func (api *AgentModelProvidersApi) Update(ctx *gin.Context) {
	var providerDTO dto.AgentModelProviderDTO
	if err := ctx.ShouldBindJSON(&providerDTO); err != nil {
		response.ParamBindError(ctx, err)
		return
	}
	if err := api.srv.Update(providerDTO); err != nil {
		response.InternalError(ctx, "修改提供商失败", err)
		return
	}
	response.SuccessMsg(ctx, "修改提供商成功")
}

func (api *AgentModelProvidersApi) Page(ctx *gin.Context) {
	// 获取分页参数
	pageRequest := paginate.GetPageParams(ctx)

	pageResult, err := api.srv.GetPage(pageRequest)
	if err != nil {
		response.InternalError(ctx, "获取提供商列表失败", err)
		return
	}

	response.SuccessWithData(ctx, pageResult)
}

func (api *AgentModelProvidersApi) SetDefault(ctx *gin.Context) {
	id, err := api.GetParamUint(ctx, "id")
	if err != nil {
		response.ParamBindError(ctx, err)
		return
	}
	if err := api.srv.SetDefault(id); err != nil {
		response.InternalError(ctx, "设置默认提供商失败", err)
		return
	}
	response.SuccessMsg(ctx, "设置默认提供商成功")
}

func (api *AgentModelProvidersApi) GetDefault(ctx *gin.Context) {
	resp, err := api.srv.GetDefaultProvider()
	if err != nil {
		response.InternalError(ctx, "获取默认提供商失败", err)
		return
	}
	response.SuccessWithData(ctx, resp)
}

func (api *AgentModelProvidersApi) GetActive(ctx *gin.Context) {
	resp, err := api.srv.GetActiveProviders()
	if err != nil {
		response.InternalError(ctx, "获取活跃提供商失败", err)
		return
	}
	response.SuccessWithData(ctx, resp)
}
